package com.rwb.springboot.sell.controller;

import com.rwb.springboot.sell.po.ProductCategory;
import com.rwb.springboot.sell.po.ProductInfo;
import com.rwb.springboot.sell.service.ProductCategoryService;
import com.rwb.springboot.sell.service.ProductInfoService;
import com.rwb.springboot.sell.utils.ResultVOUtil;
import com.rwb.springboot.sell.vo.ProductInfoVO;
import com.rwb.springboot.sell.vo.ProductVO;
import com.rwb.springboot.sell.vo.ResultVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 买家商品
 * Created by ruwenbo on 2017/12/17.
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService productCategoryService;

    @GetMapping("/list")
    public ResultVO list() {
        // 1.查询所有的上架商品
        List<ProductInfo> productInfos = productInfoService.findUpAll();
        // 2.查询所有的类目
        List<Integer> categoryTypes = new ArrayList<Integer>();
        for (ProductInfo productInfo:productInfos) {
            categoryTypes.add(productInfo.getCategoryType());
        }
        List<ProductCategory> categories = productCategoryService.findByCategoryTypeIn(categoryTypes);
        // 3.参数拼装
        List<ProductVO> productVOs = new ArrayList<ProductVO>();
        for (ProductCategory category : categories) {
            ProductVO productVO = new ProductVO();
            productVO.setCategoryName(category.getCategoryName());
            productVO.setCategoryType(category.getCategoryType());

            List<ProductInfoVO> productInfoVOS = new ArrayList<ProductInfoVO>();
            for (ProductInfo productInfo : productInfos) {
                if (productInfo.getCategoryType().equals(category.getCategoryType())) {
                    ProductInfoVO productInfoVO = new ProductInfoVO();
                    BeanUtils.copyProperties(productInfo, productInfoVO);
                    productInfoVOS.add(productInfoVO);
                }
            }
            productVO.setProductInfoVOList(productInfoVOS);
            productVOs.add(productVO);
        }


        return ResultVOUtil.success(productVOs);
    }

}
